<template>
  <el-table :data="tableData" max-height="50vh">
    <el-table-column prop="classIndex" label="课次" width="100">
      <template #default="scope">
        <el-input v-model="scope.row.classIndex" placeholder="请输入课次" />
      </template>
    </el-table-column>
    <el-table-column label="课时" align="center">
      <el-table-column prop="theoreticalClassHours" label="理论" width="120">
        <template #default="scope">
          <el-input v-model="scope.row.theoreticalClassHours" placeholder="请输入理论" />
        </template>
      </el-table-column>
      <el-table-column label="实训" width="120">
        <template #default="scope">
          <el-input v-model="scope.row.practicalTrainingHours" placeholder="请输入实训" />
        </template>
      </el-table-column>
      <el-table-column prop="address" label="模块" width="200">
        <template #default="scope">
          <el-input v-model="scope.row.modular" placeholder="请输入模块" />
        </template>
      </el-table-column>
    </el-table-column>
    <el-table-column prop="contentCourses" label="教学内容">
      <template #default="scope">
        <el-input v-model="scope.row.contentCourses" placeholder="请输入教学内容" />
      </template>
    </el-table-column>
    <el-table-column prop="lecturerName" label="任课老师" width="100" align="center" />
  </el-table>

  <div class="flex justifyCenter justifyEnd actions">
    <el-button type="primary" @click="save" :loading="loading">保存</el-button>
  </div>
</template>

<script setup lang="ts">
const { proxy } = getCurrentInstance();
let props = defineProps(["archivesId"]);

let tableData = ref([]);
let loading = ref(false);

async function save() {
  try {
    loading.value = true;
    await proxy.$apis.scheduleRelevant({
      type: "archives-tasks-save",
      data: { archivesId: props.archivesId, tasks: tableData.value },
    });
    proxy.$utils.showToast("保存成功");
    getInfo();
  } catch (error) {
  } finally {
    loading.value = false;
  }
}

async function getInfo() {
  let resp = await proxy.$apis.scheduleRelevant({
    type: "archives-tasks-list",
    data: { archivesId: props.archivesId },
  });
  console.log("教学任务书", resp);

  // let res = await proxy.$apis.scheduleRelevant({
  //   type: "archives-curriculum-list",
  //   data: { archivesId: props.archivesId },
  // });

  //如果他自己有值
  if (resp && resp.length) {
    // resp.forEach((element, index) => {
    //   if (!element.contentCourses) {
    //     element.contentCourses = res[index].curriculum;
    //   }
    // });
    tableData.value = resp;
  } else {
    let { lecturerName } = await proxy.$apis.teachingArchives({
      type: "details",
      data: { _id: props.archivesId },
    });
    let res = await proxy.$apis.scheduleRelevant({
      type: "archives-curriculum-list",
      data: { archivesId: props.archivesId },
    });
    console.log("课程列表", res);
    res.forEach((element) => {
      element.lecturerName = lecturerName;
      element.contentCourses = element.curriculum;
    });
    tableData.value = [...res];
  }
  console.log("tableData.value", tableData.value);
}

watch(
  () => props.archivesId,
  (newValue, oldValue) => {
    if (newValue) getInfo();
  },
  { immediate: true, deep: true }
);
</script>



<style lang="scss" scoped>
.actions {
  margin-top: 15px;
}
</style>

